package com.cmrx.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.cmrx.model.entity.*;
import com.cmrx.model.service.SceneBioEvidenceService;
import com.cmrx.model.service.SceneFootPrintService;
import com.cmrx.model.service.SceneVideoEvidenceService;
import com.cmrx.model.util.JwtUtil;
import com.cmrx.model.util.ResultVoUtil;
import com.cmrx.model.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/evidence")
@Api(value = "/analysis",description = "痕迹物证 接口类")
public class EvidenceController {
    //视听物证
    @Autowired
    private SceneVideoEvidenceService sceneVideoEvidenceService;
    //生物物证
    @Autowired
    private SceneBioEvidenceService sceneBioEvidenceService;
    //足迹物证
    @Autowired
    private SceneFootPrintService sceneFootPrintService;

    /******************************************获取列表**********************************************/
    @ApiOperation(value="获取物证列表信息", notes="根据url的现勘InvestigationId来指定获取物证列表信息")
    @ApiImplicitParam(name = "investigationId", value = "现勘id", required = true, dataType = "String")
    @RequestMapping(value = "/list/{investigationId}", method = RequestMethod.GET)
    public ResultVo queryBioByInvestigationId(@PathVariable("investigationId") String investigationId) {
        if(investigationId==null || "".equals(investigationId)){
            return ResultVoUtil.error(500,"现勘id不能为空");
        }

        Map<String,Object> map = new HashMap<>();
        List<SceneBioEvidence> bios =  sceneBioEvidenceService.queryByInvestigationId(investigationId);
        List<SceneVideoEvidence> videos = sceneVideoEvidenceService.queryByInvestigationId(investigationId);
        List<SceneFootPrint> foots = sceneFootPrintService.queryByInvestigationId(investigationId);
        map.put("bios",bios);
        map.put("videos",videos);
        map.put("foots",foots);
        return ResultVoUtil.success(map);
    }


    /******************************************获取详情**********************************************/
    @ApiOperation(value="获取生物物证详细信息", notes="根据url的id来指定获取生物物证详细信息")
    @ApiImplicitParam(name = "id", value = "生物物证id", required = true, dataType = "String")
    @RequestMapping(value = "/bio/{id}", method = RequestMethod.GET)
    public ResultVo queryBio(@PathVariable String id) {
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"生物物证id不能为空");
        }
        SceneBioEvidence bio =  sceneBioEvidenceService.queryById(id);
        return ResultVoUtil.success(bio);
    }

    @ApiOperation(value="获取视听物证详细信息", notes="根据url的id来指定获取视听物证详细信息")
    @ApiImplicitParam(name = "id", value = "视听物证id", required = true, dataType = "String")
    @RequestMapping(value = "/video/{id}", method = RequestMethod.GET)
    public ResultVo queryVideo(@PathVariable String id) {
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"视听物证id不能为空");
        }
        SceneVideoEvidence video = sceneVideoEvidenceService.queryById(id);
        return ResultVoUtil.success(video);
    }


    @ApiOperation(value="获取足迹物证详细信息", notes="根据url的id来指定获取足迹物证详细信息")
    @ApiImplicitParam(name = "id", value = "足迹物证id", required = true, dataType = "String")
    @RequestMapping(value = "/foot/{id}", method = RequestMethod.GET)
    public ResultVo queryFoot(@PathVariable String id) {
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"足迹物证id不能为空");
        }
        SceneFootPrint foot = sceneFootPrintService.queryById(id);
        return ResultVoUtil.success(foot);
    }



    /******************************************添加物证**********************************************/
    @ApiOperation(value="添加物证", notes="根据物证对象来创建物证")
    @ApiImplicitParam(name = "js", value = "json参数", required = true, dataType = "String")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultVo add(@RequestBody String js) {
        //返回结果
        JSONObject result = new JSONObject();

        //参数解析
        JSONObject json = JSONObject.parseObject(js);
        String token = json.getString("token");
        String investigationId = json.getJSONObject("obj").getString("investigationId");
        String type = json.getString("type");
        CommonPicture commonPicture = JSONObject.parseObject(json.getString("picture"), CommonPicture.class);
        //校验用户，图片，现勘编号
        byte[] content = null;
        try {
            String img64 = JSONObject.parseObject(json.getString("obj")).getString("content");
            if(img64==null || "".equals(img64)){
                return ResultVoUtil.error(500,"图片不能为空");
            }
            content = Base64.getDecoder().decode(img64.replaceAll("\n", "").replaceAll("\r", ""));
        }catch (Exception e){
            return ResultVoUtil.error(500,"图片格式错误");
        }
        if(token==null || "".equals(token)){
            return ResultVoUtil.error(500,"用户未登录");
        }else if(investigationId==null || "".equals(investigationId)){
            return ResultVoUtil.error(500,"现勘id不能为空");
        }

        //添加物证信息操作
        User user = JwtUtil.deToken(token);
        String id = "";
        if("视听物证".equals(type)) {
            SceneVideoEvidence videoEvidence = JSONObject.parseObject(json.getString("obj"), SceneVideoEvidence.class);
            videoEvidence.setContent(content);
            id = sceneVideoEvidenceService.addVideoEvidence(videoEvidence,user,commonPicture);
        }else if("生物物证".equals(type)) {
            SceneBioEvidence bioEvidence = JSONObject.parseObject(json.getString("obj"), SceneBioEvidence.class);
            bioEvidence.setContent(content);
            id =  sceneBioEvidenceService.addBioEvidence(bioEvidence,user,commonPicture);
        }else if("足迹物证".equals(type)){
            SceneFootPrint footPrint = JSONObject.parseObject(json.getString("obj"),SceneFootPrint.class);
            footPrint.setContent(content);
            id = sceneFootPrintService.addFootPrint(footPrint,user,commonPicture);
        }

        result.put("id",id);
        if(!"".equals(id)){
            result.put("msg","添加成功");
            return ResultVoUtil.success(result);
        }else {
            return ResultVoUtil.error(500,"添加失败");
        }
    }

    /******************************************更新修改物证**********************************************/
    @ApiOperation(value="更新物证", notes="根据url的id和type来指定更新指定的对象，并根据传过来的obj信息来更新物证详细信息")
    @ApiImplicitParam(name = "js", value = "json参数", required = true, dataType = "String")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultVo update(@RequestBody String js) {
        //返回结果
        JSONObject result = new JSONObject();

        //参数解析
        JSONObject json = JSONObject.parseObject(js);
        String id = json.getJSONObject("obj").getString("id");
        String type = json.getString("type");
        String token = json.getString("token");
        //校验用户，图片，现勘编号
        byte[] content = null;
        try {
            String img64 = JSONObject.parseObject(json.getString("obj")).getString("content");
            if(img64==null || "".equals(img64)){
                return ResultVoUtil.error(500,"更新图片不能为空");
            }
            content = Base64.getDecoder().decode(img64.replaceAll("\n", "").replaceAll("\r", ""));
        }catch (Exception e){
            return ResultVoUtil.error(500,"图片格式错误");
        }
        if(token==null || "".equals(token)){
            return ResultVoUtil.error(500,"用户未登录");
        }else if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"物证id不能为空");
        }

        //修改物证信息操作
        User user = JwtUtil.deToken(token);
        int count = 0;
        if("视听物证".equals(type)) {
            SceneVideoEvidence videoEvidence = JSONObject.parseObject(json.getString("obj"), SceneVideoEvidence.class);
            videoEvidence.setContent(content);
            count = sceneVideoEvidenceService.updateById(videoEvidence,user);
        }else if("生物物证".equals(type)){
            SceneBioEvidence bioEvidence = JSONObject.parseObject(json.getString("obj"), SceneBioEvidence.class);
            bioEvidence.setContent(content);
            count = sceneBioEvidenceService.updateById(bioEvidence,user);
        }else if("足迹物证".equals(type)){
            SceneFootPrint sceneFootPrint = JSONObject.parseObject(json.getString("obj"),SceneFootPrint.class);
            sceneFootPrint.setContent(content);
            count = sceneFootPrintService.updateById(sceneFootPrint,user);
        }

        if(count>0){
            result.put("msg","修改成功");
            return ResultVoUtil.success(result);
        }else {
            return ResultVoUtil.error(500,"修改失败");
        }
    }

    /******************************************删除物证**********************************************/
    @ApiOperation(value="删除视听物证", notes="根据url的id来指定视听物证")
    @ApiImplicitParam(name = "id", value = "视听物证ID", required = true, dataType = "String")
    @RequestMapping(value = "/deleteVideo/{id}", method = RequestMethod.GET)
    public ResultVo deleteVideo(@PathVariable String id) {
        //返回结果
        JSONObject result = new JSONObject();
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"视听物证id不能为空");
        }

        int count = sceneVideoEvidenceService.deleteById(id);
        if(count>0){
            result.put("msg","刪除成功");
            return ResultVoUtil.success(result);
        }else {
            return ResultVoUtil.error(500,"视听删除失败");
        }
    }

    @ApiOperation(value="删除生物物证", notes="根据url的id来指定生物物证")
    @ApiImplicitParam(name = "id", value = "视听物证ID", required = true, dataType = "String")
    @RequestMapping(value = "/deleteBio/{id}", method = RequestMethod.GET)
    public ResultVo deleteBio(@PathVariable String id) {
        //返回结果
        JSONObject result = new JSONObject();
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"生物物证id不能为空");
        }

        int count = sceneBioEvidenceService.deleteById(id);
        if(count>0){
            result.put("msg","刪除成功");
            return ResultVoUtil.success(result);
        }else {
            return ResultVoUtil.error(500,"生物删除失败");
        }
    }



    @ApiOperation(value="删除足迹物证", notes="根据url的id来指定足迹物证")
    @ApiImplicitParam(name = "id", value = "足迹物证ID", required = true, dataType = "String")
    @RequestMapping(value = "/deleteFoot/{id}", method = RequestMethod.GET)
    public ResultVo deleteFoot(@PathVariable String id) {
        //返回结果
        JSONObject result = new JSONObject();
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(500,"足迹物证id不能为空");
        }

        int count = sceneFootPrintService.deleteById(id);
        if(count>0){
            result.put("msg","刪除成功");
            return ResultVoUtil.success(result);
        }else {
            return ResultVoUtil.error(500,"足迹删除失败");
        }
    }
}
